<template>
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item>
          <el-button @click="openlenreDialog" type="primary"
            >查看冷热数据</el-button
          >
          <el-button @click="opendialogqishulenre" type="primary"
            >冷热期数数据</el-button
          >
          <el-button @click="reload" type="primary">刷新</el-button>
        </el-form-item>

        <el-form-item>
          <el-popover placement="top" v-model="deleteVisible" width="160">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button @click="deleteVisible = false" size="mini" type="text"
                >取消</el-button
              >
              <el-button @click="onDelete" size="mini" type="primary"
                >确定</el-button
              >
            </div>
            <el-button
              icon="el-icon-delete"
              size="mini"
              slot="reference"
              type="danger"
              >批量删除</el-button
            >
          </el-popover>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      border
      ref="multipleTable"
      stripe
      :height="600"
      tooltip-effect="dark"
    >
      <el-table-column label="ID" prop="ID" align="center"></el-table-column>
      <el-table-column label="time" prop="time" width="120"></el-table-column>

      <el-table-column
        label="number1字段"
        prop="number1"
        width="120"
      ></el-table-column>

      <el-table-column
        label="number2字段"
        prop="number2"
        width="120"
      ></el-table-column>

      <el-table-column
        label="number3字段"
        prop="number3"
        width="120"
      ></el-table-column>

      <el-table-column
        label="number4字段"
        prop="number4"
        width="120"
      ></el-table-column>

      <el-table-column
        label="number5字段"
        prop="number5"
        width="120"
      ></el-table-column>

      <el-table-column label="所属项目" prop="projectId" width="120">
        <template slot-scope="scope">
          {{ filterDict(scope.row.projectId, "sys_project_id") }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            class="table-button"
            @click="updateSysCaipiaoNumbers(scope.row)"
            size="small"
            type="primary"
            icon="el-icon-edit"
            >变更</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteRow(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :before-close="closeDialog"
      :visible.sync="dialogFormVisible"
      title="弹窗操作"
    >
      <el-form :model="formData" label-position="right" label-width="80px">
        <el-form-item label="time字段:">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="formData.time"
            clearable
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="number1字段:">
          <el-input
            v-model="formData.number1"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="number2字段:">
          <el-input
            v-model="formData.number2"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="number3字段:">
          <el-input
            v-model="formData.number3"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="number4字段:">
          <el-input
            v-model="formData.number4"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="number5字段:">
          <el-input
            v-model="formData.number5"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="projectId字段:">
          <el-select
            v-model="formData.projectId"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(item, key) in sys_project_idOptions"
              :key="key"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :before-close="closelengreDialog"
      :visible.sync="dialoglenreFormVisible"
      title="冷热数据(539统计)"
    >
      <el-form :model="formData" label-position="right" label-width="80px">
        <span> {{ messageA }}</span>
        <span> {{ messageB }}</span>
        <span> {{ messageC }}</span>
        <el-form-item label="统计期数:">
          <el-input
            v-model="formData.qishu"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-form>

      <el-table
        :data="formlenreData"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        :default-sort="{ prop: 'count', order: 'descending' }"
      >
        <el-table-column prop="number" label="号码" width="200">
        </el-table-column>
        <el-table-column prop="count" label="次数" width="200" sortable>
        </el-table-column>
      </el-table>

      <div class="dialog-footer" slot="footer">
        <el-button @click="closelengreDialog">关闭</el-button>
        <el-button @click="getlenre" type="primary">搜索</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :before-close="closeqishulengreDialog"
      :visible.sync="dialogqishulenreFormVisible"
      title="冷热期数数据(539统计)"
    >
      <el-form :model="formData" label-position="right" label-width="80px">
        <span> {{ messageA }}</span>
        <span> {{ messageB }}</span>
        <span> {{ messageC }}</span>
        <el-form-item label="起始期数:">
          <el-input
            v-model="formData.minqishu"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="结束期数:">
          <el-input
            v-model="formData.maxqishu"
            clearable
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-form>

      <el-table
        :data="formlenreData"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        :default-sort="{ prop: 'count', order: 'descending' }"
      >
        <el-table-column prop="number" label="号码" width="200">
        </el-table-column>
        <el-table-column prop="count" label="次数" width="200" sortable>
        </el-table-column>
      </el-table>

      <div class="dialog-footer" slot="footer">
        <el-button @click="closeqishulengreDialog">关闭</el-button>
        <el-button @click="getqishu" type="primary">搜索</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createSysCaipiaoNumbers,
  deleteSysCaipiaoNumbers,
  deleteSysCaipiaoNumbersByIds,
  updateSysCaipiaoNumbers,
  findSysCaipiaoNumbers,
  getSysCaipiaoNumbersList,
  getLengSysCaipiaoNumbers,
} from "@/api/sysCaipiaoNumbers"; //  此处请自行替换地址
import { formatTimeToStr } from "@/utils/date";
import infoList from "@/mixins/infoList";
export default {
  name: "SysCaipiaoNumbers",
  mixins: [infoList],
  data() {
    return {
      listApi: getSysCaipiaoNumbersList,
      dialogFormVisible: false, //创建的期数
      dialoglenreFormVisible: false, //倒数统计
      dialogqishulenreFormVisible: false, //期数统计
      type: "",
      deleteVisible: false,
      multipleSelection: [],
      sys_project_idOptions: [],
      formData: {
        time: new Date(),
        number1: "",
        number2: "",
        number3: "",
        number4: "",
        number5: "",
        projectId: 0,
        project_id: 539,
        qishu: 3000,
        minqishu: "",
        maxqishu: "",
      },
      formlenreData: [],
      messageA: "期数请输入103/11/01格式，尤其在下方表格需要找到",
      messageB: "牛逼",
      messageC: "牛逼",
      datadict: {},
    };
  },
  filters: {
    formatDate: function (time) {
      if (time != null && time != "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
      } else {
        return "";
      }
    },
    formatBoolean: function (bool) {
      if (bool != null) {
        return bool ? "是" : "否";
      } else {
        return "";
      }
    },
  },
  methods: {
    //条件搜索前端看此方法
    reload() {
      this.$router.go(0);
    },
    onSubmit() {
      this.page = 1;
      this.pageSize = 10;
      this.getTableData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteRow(row) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.deleteSysCaipiaoNumbers(row);
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 1) {
        return "warning-row";
      } else if (rowIndex === 3) {
        return "success-row";
      }
      return "";
    },
    async onDelete() {
      const ids = [];
      if (this.multipleSelection.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择要删除的数据",
        });
        return;
      }
      this.multipleSelection &&
        this.multipleSelection.map((item) => {
          ids.push(item.ID);
        });
      const res = await deleteSysCaipiaoNumbersByIds({ ids });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        if (this.tableData.length == ids.length && this.page > 1) {
          this.page--;
        }
        this.deleteVisible = false;
        this.getTableData();
      }
    },
    async updateSysCaipiaoNumbers(row) {
      const res = await findSysCaipiaoNumbers({ ID: row.ID });
      this.type = "update";
      if (res.code == 0) {
        this.formData = res.data.resysCaipiaoNumbers;
        this.dialogFormVisible = true;
      }
    },

    closeDialog() {
      this.dialogFormVisible = false;
      this.formData = {
        time: new Date(),
        number1: "",
        number2: "",
        number3: "",
        number4: "",
        number5: "",
        projectId: 0,
      };
    },
    closelengreDialog() {
      this.formlenreData = [];
      this.dialoglenreFormVisible = false;
    },

    closeqishulengreDialog() {
      this.formlenreData = [];
      this.dialogqishulenreFormVisible = false;
    },

    async deleteSysCaipiaoNumbers(row) {
      const res = await deleteSysCaipiaoNumbers({ ID: row.ID });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        if (this.tableData.length == 1 && this.page > 1) {
          this.page--;
        }
        this.getTableData();
      }
    },
    async enterDialog() {
      let res;
      switch (this.type) {
        case "create":
          res = await createSysCaipiaoNumbers(this.formData);
          break;
        case "update":
          res = await updateSysCaipiaoNumbers(this.formData);
          break;
        default:
          res = await createSysCaipiaoNumbers(this.formData);
          break;
      }
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "创建/更改成功",
        });
        this.closeDialog();
        this.getTableData();
      }
    },
    openDialog() {
      this.type = "create";
      this.dialogFormVisible = true;
    },
    openlenreDialog() {
      this.dialoglenreFormVisible = true;
    },
    opendialogqishulenre() {
      this.dialogqishulenreFormVisible = true;
    },
    getObjValue(obj) {
      //把对象属性的值转换为array
      var arr = [];
      for (let i in obj) {
        arr.push(obj[i]); //属性
      }
      return arr;
    },
    getObjsToArray(objs) {
      //把大量对象的属性转化为列表。
      for (let obj in objs) {
        objs[obj]["dataArrays"] = this.getObjValue(objs[obj]);
      }
      window.console.log(objs);
      return objs;
    },
    async getlenre() {
      //截取尾部数组
      this.formlenreData = [];
      const qi = parseInt(this.formData.qishu);
      let tableArray = [];
      if (qi <= this.tableData.length) {
        this.tableData = this.tableData.slice(-1 * qi);
      }
      window.console.log(this.tableData);
      //转化未数组
      tableArray = this.getObjsToArray(this.tableData);
      for (let obj in tableArray) {
        let dataArrays = tableArray[obj]["dataArrays"];
        for (let i = 4; i < 9; i++) {
          let key = dataArrays[i];
          if (this.datadict.hasOwnProperty(key)) {
            this.datadict[key] = this.datadict[key] + 1;
          } else {
            this.datadict[key] = 1;
          }
        }
      }
      window.console.log(this.datadict);
      for (let k in this.datadict) {
        this.formlenreData.push({
          number: k,
          count: this.datadict[k],
        });
      }
      this.setmessage();
    },
    setmessage() {
      //设置通用的提示信息
      let endtableData = this.tableData.splice(-1)[0];
      this.messageA =
        "当前数据期数为：" +
        this.tableData[0]["ID"] +
        "------" +
        endtableData["ID"];
      this.formlenreData;
      let sorted_keys_array = Object.keys(this.datadict).sort((a, b) => {
        return this.datadict[b] - this.datadict[a];
      });
      window.console.log(sorted_keys_array);
      let aindex = 3;
      let bindex = -3;
      let rearry = [];
      let lenarry = [];
      if (aindex < sorted_keys_array.length) {
        rearry = sorted_keys_array.splice(0, aindex);
        lenarry = sorted_keys_array.splice(bindex);
      } else {
        rearry = sorted_keys_array;
      }

      this.messageB = "当前最热的数为：" + rearry.join(" ");
      this.messageC = "当前最冷的数为：" + lenarry.join(" ");
    },
    async getqishu() {
      //截取尾部数组
      this.formlenreData = [];
      window.console.log(this.tableData);
      //转化未数组
      let startindex = "not";
      let endindex = "not";
      for (let obj in this.tableData) {
        let dataArrays = this.tableData[obj];
        if (dataArrays["time"] == this.formData.minqishu) {
          startindex = obj;
        }
        if (dataArrays["time"] == this.formData.maxqishu) {
          endindex = obj;
          break;
        }
        window.console.log(
          dataArrays["time"],
          this.formData.minqishu,
          this.formData.maxqishu
        );
      }
      if (startindex == "not" || endindex == "not") {
        alert("找不到对应的期数！请仔细检查期数输出是否正常！");
        return false;
      }
      this.tableData = this.tableData.slice(startindex, parseInt(endindex) + 1);
      let tableArray = this.getObjsToArray(this.tableData);

      for (let obj in tableArray) {
        let dataArrays = tableArray[obj]["dataArrays"];
        for (let i = 4; i < 9; i++) {
          let key = dataArrays[i];
          if (this.datadict.hasOwnProperty(key)) {
            this.datadict[key] = this.datadict[key] + 1;
          } else {
            this.datadict[key] = 1;
          }
        }
      }
      window.console.log(this.datadict);
      for (let k in this.datadict) {
        this.formlenreData.push({
          number: k,
          count: this.datadict[k],
        });
      }
      this.setmessage();
    },
  },

  async created() {
    this.page = 1;
    this.pageSize = 100000;
    await this.getTableData();
    await this.getDict("sys_project_id");
  },
};
</script>

<style>
</style>
